<?php
/**
 * Ora - 后台 自定义页面 管理模块
 * @author lan7
 * @copyright lan7.net
 * @version 2.0
 */

if(!defined('IN_ORA')) {
	exit('Access Denied');
}
class admin_page extends mod{
	function admin_page(){
		parent::__construct();
		session_start();
		$this->setTplDir(PUBLIC_PATH);
		$this->setMsgTpl("admin/msg.html");
		if(!isset($_SESSION[$GLOBALS['ora_cfg']['sess_prefix']."admin"])){
			$this->showMsg("请先登录!",parse_uri("common-login"),1);
		}
	}

	/**
	 * 列表页
	 *
	 */
	function index(){
		$this->assign('pages',$this->db->getAll('select * from '.tn('pages').' order by id desc'));
		$this->display("admin/pages.html");
	}

	/**
	 * 编辑页
	 *
	 */
	function modify(){
		$id = $GLOBALS['_param']['id'];
		if(is_numeric($id)){
			$page = $this->db->getOne("select ops.*,op.content,op.id as content_id from ".tn("pages")." ops left join ".tn('page')." op on ops.id = op.pid where ops.id = $id");
			if($page){
				$this->assign('page',$page);
			}else{
				$this->showMsg('未找到记录!');
			}
		}
		$this->display("admin/pages.html");
	}

	/**
	 * 保存
	 *
	 */
	function save(){
		$pid = $_POST['page_id'];
		$name = $_POST['name'];
		if(empty($name)){
			$this->showMsg('请输入页面名称!',$_SERVER['HTTP_REFERER'],1);
		}
		$ptype = $_POST['ptype']=='1'?1:0;
		$is_nav = $_POST['is_nav']=='1'?1:0;
		if($ptype=='1'){
			$url = $_POST['url'];
			if(is_numeric($pid)){
				$sql = "update ".tn('pages')." set `name` = '$name',ptype = $ptype , url = '$url',is_nav = $is_nav,dateline = ".time()." where id = $pid";
				$this->db->execute($sql);
			}else{
				$sql = "insert into ".tn('pages')."(`name`,ptype,url,dateline,is_nav) values('$name',$ptype,'$url',".time().",$is_nav)";
				$this->db->execute($sql);
				$pid = $this->db->getLastInsId();
			}
			$this->db->execute("delete from ".tn('page')." where pid = $pid ");
			$this->showMsg('操作成功!',parse_uri('admin_page-index'),1);
		}else{
			$content = htmlspecialchars($_POST['content']);
			$content_id = $_POST['content_id'];
			if(is_numeric($pid)){
				$sql = "update ".tn('pages')." set `name` = '$name',ptype = $ptype,is_nav = $is_nav,dateline = ".time()." where id = $pid";
				$this->db->execute($sql);
			}else{
				$sql = "insert into ".tn('pages')."(`name`,ptype,dateline,is_nav) values('$name',$ptype,".time().",$is_nav)";
				$this->db->execute($sql);
				$pid = $this->db->getLastInsId();
			}
			$sql = "insert into ".tn('page')."(ptype,content,pid) values($ptype,'$content',$pid)";
			if(is_numeric($content_id)){
				$sql = "update ".tn('page')." set ptype=$ptype,pid = $pid,content = '$content' where Id = $content_id";
			}
			$this->db->execute($sql);
			$this->showMsg('操作成功!',parse_uri('admin_page-index'),1);
		}
	}

	function add(){
		$this->modify();
	}
	/**
	 * 删除
	 *
	 */
	function del(){
		$id = $GLOBALS['_param']['id'];
		if(is_numeric($id)){
			$this->db->execute("delete from ".tn('pages')." where id = $id");
			$this->db->execute("delete from ".tn('page')." where pid = $id");
			$this->showMsg('操作成功!',parse_uri('admin_page-index'),1);
		}else{
			$this->showMsg('非法参数!',parse_uri('admin_page-index'),1);
		}
	}

}
?>